/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farent.solutions.plugins.stripes.hyperstripes.core;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author farouka
 */
public class HSDOMHandler
{
    public static void replaceComponentsToFile( File fis, DefaultComponentModel root ) throws
            ParserConfigurationException, SAXException, IOException, TransformerConfigurationException, TransformerException
    {
        FileInputStream fileInputStream = new FileInputStream(fis);
        String xmlString = replaceCoreComponents( fileInputStream, root );
        BufferedWriter out = new BufferedWriter(new FileWriter("page.temp"));
        out.write( xmlString );
        out.close();
    }

    public static String replaceCoreComponents( InputStream fis, ComponentModel root ) throws
            ParserConfigurationException, SAXException, IOException, TransformerConfigurationException, TransformerException
    {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.
                newInstance();

        DocumentBuilder documentBuilder = documentBuilderFactory.
                newDocumentBuilder();

        Document document = documentBuilder.parse( fis );

        for ( ComponentModel child : root.getChildren())
        {
            String id = child.getId();
            NodeList components = document.getElementsByTagName(child.getName());
            for ( int i = 0; i < components.getLength(); i ++ )
            {
                Node component = components.item( i );
                 NamedNodeMap attr = component.getAttributes();
                 Node attrId = attr.getNamedItem("id");
                 if(attrId.getNodeValue().equalsIgnoreCase( id ))
                 {
                    Element div = document.createElement("div");
                    div.setAttribute( "id", id );
                    Node parent = component.getParentNode();
                    parent.replaceChild( div, component );
                 }
            }
        }
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(document);
        transformer.transform(source, result);

        return result.getWriter().toString();
        
    }
}
